﻿using System.Collections.Generic;
using System.Xml.Linq;
using Newtonsoft.Json;

namespace T.FAS.Runtime.MF.Entity
{
    public class Microflow
    {
        [JsonProperty("ID")]
        public string Id { get; set; }

        [JsonProperty("ItemID")]
        public string ItemId { get; set; }

        [JsonProperty("ItemName")]
        public string ItemName { get; set; }

        [JsonProperty("ItemDescription")]
        public string ItemDescription { get; set; }

        [JsonProperty("ItemIcon")]
        public string ItemIcon { get; set; }

        [JsonProperty("DesignData")]
        public DesignData DesignData { get; set; }

        public virtual List<UserVariable> NodeInputOutputVariables
        {
            get
            {
                var variables = new List<UserVariable>();
                foreach (var item in DesignData.Cells)
                {
                    //item.NodeInputOutputVariables.ForEach(v => v.NodeID = item.Id);
                    variables.AddRange(item.NodeInputOutputVariables);
                }
                return variables; 
            }
        }
    }

    // 工作流容器类
    public class DesignData
    {
        [JsonProperty("cells")]
        public List<ShapeBase> Cells { get; set; }
    }

    // 以下为支持类
    public class EdgeAttributes
    {
        [JsonProperty("line")]
        public LineStyle Line { get; set; }
    }

    public class LineStyle
    {
        [JsonProperty("stroke")]
        public string Stroke { get; set; }

        [JsonProperty("strokeDasharray")]
        public string StrokeDashArray { get; set; }
    }

    public class ConnectionPoint
    {
        [JsonProperty("cell")]
        public string Cell { get; set; }

        [JsonProperty("port")]
        public string Port { get; set; }
    }

    public class Position
    {
        [JsonProperty("x")]
        public int X { get; set; }

        [JsonProperty("y")]
        public int Y { get; set; }
    }

    public class Size
    {
        [JsonProperty("width")]
        public int Width { get; set; }

        [JsonProperty("height")]
        public int Height { get; set; }
    }

    public class Ports
    {
        [JsonProperty("groups")]
        public Dictionary<string, PortGroup> Groups { get; set; }

        [JsonProperty("items")]
        public List<PortItem> Items { get; set; }
    }

    public class PortGroup
    {
        [JsonProperty("label")]
        public Label Label { get; set; }

        [JsonProperty("position")]
        public object Position { get; set; } // 可以是字符串或对象

        [JsonProperty("attrs")]
        public Dictionary<string, PortAttribute> Attrs { get; set; }

        [JsonProperty("markup")]
        public List<Markup> Markup { get; set; }
    }

    public class Label
    {
        [JsonProperty("position")]
        public string Position { get; set; }
    }

    public class PortAttribute
    {
        [JsonProperty("r", NullValueHandling = NullValueHandling.Ignore)]
        public int? Radius { get; set; }

        [JsonProperty("magnet", NullValueHandling = NullValueHandling.Ignore)]
        public bool? Magnet { get; set; }

        // 其他属性根据实际需要添加
    }

    public class Markup
    {
        [JsonProperty("tagName")]
        public string TagName { get; set; }

        [JsonProperty("selector")]
        public string Selector { get; set; }
    }

    public class PortItem
    {
        [JsonProperty("group")]
        public string Group { get; set; }

        [JsonProperty("id")]
        public string Id { get; set; }

        [JsonProperty("attrs")]
        public Dictionary<string, PortAttribute> Attrs { get; set; }
    }

    // 变量类
    public class UserVariable
    {
        private string _variableValue;
        [JsonProperty("variableId")]
        public string ID { get; set; }

        [JsonProperty("nodeId")]
        public string NodeID { get; set; }

        [JsonProperty("codeParamName")]
        public string CodeParamName { get; set; }

        [JsonProperty("variable")]
        public string VariableName { get; set; }

        [JsonProperty("type")]
        public string Type { get; set; }

        [JsonProperty("label")]
        public string Description { get; set; }

        [JsonProperty("valueType")]
        public string ValueType { get; set; }

        [JsonProperty("variableNodeId")]
        public string VariableNodeID { get; set; }
        [JsonProperty("variableValue")]
        public string VariableValue
        {
            get => _variableValue; set
            {
                // 首次反序列化时，OriginalVariableValue 为 null，将其备份
                if (OriginalVariableValue == null)
                {
                    OriginalVariableValue = value;
                }
                _variableValue = value;
            }
        }
        [JsonProperty("reference")]
        public string Reference { get; set; }
        [JsonProperty("variableType")]

        public string VariableType { get; set; }


        [JsonProperty("options")]
        public List<string> Options { get; set; }

        //[JsonProperty("sourceNode")]
        //public string SourceNode { get; set; }
        [JsonIgnore]
        public string OriginalVariableValue { get; set; }

        /// <summary>
        /// 变量所属activity的ID
        /// </summary>
        public string ActivityID { get; set; }

        public string CompositeVarName { get; set; }
    }

    
}